package com.cloud.search.controller;

import com.cloud.search.document.ProfileDocument;
import com.cloud.search.service.ProfileService;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/search")
public class ProfileController {

    @Autowired
    private RestHighLevelClient client;

    private ProfileService service;

    @Autowired
    public ProfileController(ProfileService service) {

        this.service = service;
    }

    @GetMapping("/test")
    public String test(){

//        Map<String, Object> jsonMap = new HashMap<>();
//        jsonMap.put("user", "laimailai");
//        jsonMap.put("postDate", new Date());
//        jsonMap.put("message", "trying out Elasticsearch");
//        IndexRequest indexRequest = new IndexRequest("test").id("2")
//                .source(jsonMap);
//        try {
//            IndexResponse indexResponse = client.index(indexRequest,SearchEsConfig.COMMON_OPTIONS);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

//        GetRequest request = new GetRequest(
//                "test",
//                "1");
//
//        try {
//            GetResponse documentFields = client.get(request, SearchEsConfig.COMMON_OPTIONS);
//            System.out.println(JSON.toJSONString(documentFields));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        try{
            // 1、创建 创建索引request 参数：索引名java
            CreateIndexRequest request = new CreateIndexRequest("shop");
            // 2、设置索引的settings
            request.settings(Settings.builder().put("index.number_of_shards", 1) // 分片数
                    .put("index.number_of_replicas", 0) // 副本数
            );
            
            // 3、设置索引的mappings
            request.mapping("_doc","{\n" +
                            "  \"_doc\":{\n" +
                            "            \"properties\":{\n" +
                            "                \"name\":{\n" +
                            "                    \"type\":\"keyword\"\n" +
                            "                },\n" +
                            "                \"message\":{\n" +
                            "                    \"type\":\"text\"\n" +
                            "                },\n" +
                            "                \"age\":{\n" +
                            "                    \"type\":\"integer\"\n" +
                            "                }\n" +
                            "            }\n" +
                            "        }\n" +
                            "}",

                    XContentType.JSON);
            // 4、 设置索引的别名
            request.alias(new Alias("it"));
            RequestOptions requestOptions = RequestOptions.DEFAULT;
            // 5、 发送请求
            // 5.1 同步方式发送请求
            CreateIndexResponse createIndexResponse = client.indices().create(request,requestOptions);

            // 6、处理响应
            boolean acknowledged = createIndexResponse.isAcknowledged();

            boolean shardsAcknowledged = createIndexResponse

                    .isShardsAcknowledged();

            System.out.println("acknowledged = " + acknowledged);

            System.out.println("shardsAcknowledged = " + shardsAcknowledged);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Success";
    }

    @PostMapping("/profiles")
    public ResponseEntity createProfile(@RequestBody ProfileDocument document) throws Exception {

        return new ResponseEntity(service.createProfileDocument(document), HttpStatus.CREATED);
    }

    @PutMapping
    public ResponseEntity updateProfile(@RequestBody ProfileDocument document) throws Exception {

        return new ResponseEntity(service.updateProfile(document), HttpStatus.CREATED);
    }

    @GetMapping("/{id}")
    public ProfileDocument findById(@PathVariable String id) throws Exception {

        return service.findById(id);
    }

    @GetMapping("/findAll")
    public List<ProfileDocument> findAll() throws Exception {

        return service.findAll();
    }

    @GetMapping(value = "/search")
    public List<ProfileDocument> search(@RequestParam(value = "technology") String technology) throws Exception {
        return service.searchByTechnology(technology);
    }

    @GetMapping(value = "/api/v1/profiles/name-search")
    public List<ProfileDocument> searchByName(@RequestParam(value = "name") String name) throws Exception {
        return service.findProfileByName(name);
    }


    @DeleteMapping("/{id}")
    public String deleteProfileDocument(@PathVariable String id) throws Exception {

        return service.deleteProfileDocument(id);

    }


}
